#coding:utf-8
from django.shortcuts import render, HttpResponse
import datetime
from django.core.cache import cache


# @cache_page(60*30)
def index(request):
    return HttpResponse("Hello World!")


def test_cache(request):
    now =   datetime.datetime.now()
    second = now.second
    # now = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')

    key = 'titles'
    titles_cached = cache.get(key) #获取缓存中key对应的值

    if not titles_cached: #如果缓存中没有数据，使用默认数据，并将输入存入缓存
        titles = [(second, 'Gavin'), (second, 'Michael'), (second, 'Jack')]
        cache.set(key, titles)   # 将titles添加到缓存中
    else:#使用缓存中的数据
        titles = titles_cached
    context = {'now':datetime.datetime.strftime(now,'%Y-%m-%d %H:%M:%S'),'second': second, 'titles': titles, 'cached': titles_cached}
    return render(request, 'cache.html', context)
